package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SetmealMapper setmealMapper;


    /**
     * 营业额：当日已完成订单的总金额
     * 有效订单：当日已完成订单的数量
     * 订单完成率：有效订单数 / 总订单数
     * 平均客单价：营业额 / 有效订单数
     * 新增用户：当日新增用户的数量
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDate begin, LocalDate end) {
        LocalDate date = LocalDate.now().minusYears(11);
        if (end .equals(date) ) {
            //获取订单数据
            Map map = new HashMap();
            map.put("beginTime", LocalDateTime.of(begin, LocalTime.MIN));
            map.put("endTime", LocalDateTime.of(end, LocalTime.MAX));
            //查询总订单数
            String totalOrderCount = orderMapper.selectOrderByDay(map);

            map.put("status", Orders.COMPLETED);
            //营业额
            Double turnover = orderMapper.getTurnoverByDate(map);
            turnover = turnover == null ? 0.0 : turnover;

            //有效订单数
            String validOrderCount = orderMapper.selectUseOrderByDay(map);

            Double unitPrice = 0.0;

            Double orderCompletionRate = 0.0;
            if (totalOrderCount != null && validOrderCount != null) {
                //订单完成率
                orderCompletionRate = Double.valueOf(validOrderCount) / Double.valueOf(totalOrderCount);
                //平均客单价
                unitPrice = Double.valueOf(turnover) / Double.valueOf(validOrderCount);
            }

            //新增用户数
            String newUsers = userMapper.selectNewUser(map);

            return BusinessDataVO.builder()
                    .turnover(turnover)
                    .validOrderCount(Integer.parseInt(validOrderCount))
                    .orderCompletionRate(orderCompletionRate)
                    .unitPrice(unitPrice)
                    .newUsers(Integer.parseInt(newUsers))
                    .build();
        } else {

            Map map = new HashMap();
            map.put("beginTime", LocalDateTime.of(begin, LocalTime.MIN));
            map.put("endTime", LocalDateTime.of(end, LocalTime.MAX));
            //查询总订单数
            String totalOrderCount = orderMapper.selectUseOrderByDay(map);

            map.put("status", Orders.COMPLETED);
            //营业额
            Double turnover = orderMapper.getTurnoverByDate(map);
            turnover = turnover == null ? 0.0 : turnover;

            //有效订单数
            String validOrderCount = orderMapper.selectUseOrderByDay(map);

            Double unitPrice = 0.0;

            Double orderCompletionRate = 0.0;
            if (totalOrderCount != null && validOrderCount != null) {
                //订单完成率
                orderCompletionRate = Double.valueOf(validOrderCount) / Double.valueOf(totalOrderCount);
                //平均客单价
                unitPrice = Double.valueOf(turnover) / Double.valueOf(validOrderCount);
            }

            //新增用户数
            String newUsers = userMapper.selectNewUser(map);

            return BusinessDataVO.builder()
                    .turnover(turnover)
                    .validOrderCount(Integer.parseInt(validOrderCount))
                    .orderCompletionRate(orderCompletionRate)
                    .unitPrice(unitPrice)
                    .newUsers(Integer.parseInt(newUsers))
                    .build();
        }

    }


    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessDate(LocalDate begin) {
        LocalDateTime begins = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(begin, LocalTime.MAX);
        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", begins);
        map.put("endTime", end);
        //新增用户数
        String newUser = userMapper.selectNewUser(map);

        //营业而
        map.put("status", Orders.COMPLETED);

        BusinessDataVO businessDataVO = orderMapper.selectBusinessDate(map);
        businessDataVO.setNewUsers(Integer.parseInt(newUser));
        return businessDataVO;

    }

    /**
     * 查询订单总起用数量
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        SetmealOverViewVO setmeal = setmealMapper.selectOverviewSetmeals();
        return setmeal;
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        DishOverViewVO dish = setmealMapper.selectOverviewDish();
        return dish;
    }

    /**
     * 查询订单管理
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        return setmealMapper.selectOverviewOrders();

    }
}
